package com.my.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.my.R;
import com.my.util.ApiUtil;

public class NightSpotActivity extends AppCompatActivity {

    TextView dateTime;
    EditText remarks;
    DatePicker datePicker;
    Button submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_night_spot);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
        }
        dateTime = findViewById(R.id.date_time);
        datePicker = findViewById(R.id.date_picker);
        remarks = findViewById(R.id.remarks);
        submit = findViewById(R.id.submit);
        JSONObject params = new JSONObject();
        dateTime.setOnClickListener(view -> {
            datePicker.setVisibility(View.VISIBLE);
            submit.setVisibility(View.GONE);
        });
        datePicker.setOnDateChangedListener((v, y, m, d) -> {
            params.putOpt("dateTime", y + "-" + (m + 1) + "-" + d);
            dateTime.setText(y + "-" + (m + 1) + "-" + d);
            datePicker.setVisibility(View.GONE);
            submit.setVisibility(View.VISIBLE);
        });
        Handler handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                JSONObject rest = ((JSONObject) msg.obj);
                Toast.makeText(NightSpotActivity.this, rest.getStr("msg"), Toast.LENGTH_LONG).show();
                if(rest.getInt("code") == 200) {
                    onBackPressed();
                }
            }
        };
        submit.setOnClickListener(view -> {
            params.putOpt("remarks", remarks.getText());
            SharedPreferences preferences = this.getSharedPreferences("LOGIN_DATA", Context.MODE_PRIVATE);
            JSONObject user = new JSONObject(preferences.getString("user", null));
            params.putOpt("userId", user.getInt("id"));
            if(params.size() < 3) {
                Toast.makeText(this, "请完善参数", Toast.LENGTH_LONG).show();
                return;
            }
            new Thread(() -> {
                Message message = new Message();
                String body = HttpRequest.post(ApiUtil.HTTP_ADDRESS + "lateCome/add").body(params.toString()).execute().body();
                message.obj = new JSONObject(body);
                handler.sendMessage(message);
            }).start();
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == 16908332) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
}